package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.FishDetailsDaoImpl;
import com.ysd.entity.Details;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminProductServlet extends BaseServlet {
	/**
	 * 产品分页多条件查询
	 * localhost:8080/shangcheng1/adminProduct?reqName=getAdminProductByWhere
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getAdminProductByWhere(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 接受前台传来的参数      非空判定
		
		
		int fishId = 0;//产品id
		if(request.getParameter("fishId")!=null && !request.getParameter("fishId").equals("")) {
			fishId = Integer.parseInt(request.getParameter("fishId"));
		}
		
		int beginPrice = 0;//产品初始价格
		if(request.getParameter("beginPrice")!=null && !request.getParameter("beginPrice").equals("")) {
			beginPrice = Integer.parseInt(request.getParameter("beginPrice"));
		}
		
		int endPrice = 0;//产品最终价格
		if(request.getParameter("endPrice")!=null && !request.getParameter("endPrice").equals("")) {
			endPrice = Integer.parseInt(request.getParameter("endPrice"));
		}

		int fishClassifyId = 0;//产品所属分类
		if(request.getParameter("fishClassifyId")!=null && !request.getParameter("fishClassifyId").equals("")) {
			fishClassifyId = Integer.parseInt(request.getParameter("fishClassifyId"));
		}
		
		String fishName = request.getParameter("fishName");//产品名称
		String fishAdress = request.getParameter("fishAdress");//产品分布
		String fishLifeTime = request.getParameter("fishLifeTime");//产品寿命
		String isFreeDelivery = request.getParameter("isFreeDelivery");//是否包邮
		String fishPicture = request.getParameter("fishPicture");//产品图片
		
		Details d = new Details(fishId, fishName, fishAdress, 0, beginPrice, endPrice, fishLifeTime, isFreeDelivery, fishClassifyId, fishPicture, "");
		
		FishDetailsDaoImpl fDao = new FishDetailsDaoImpl();

		
		List<Details> dList = fDao.selProductByWhere(d);//调用实现类中的方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(dList.size());
		out.print(res);
		
		out.flush();
		out.close();
	}

	/**
	 * 产品分页多条件查询
	 * localhost:8080/shangcheng1/adminProduct?reqName=getAdminProductByPage
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getAdminProductByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 接受前台传来的参数      非空判定
		
		
		int fishId = 0;//产品id
		if(request.getParameter("fishId")!=null && !request.getParameter("fishId").equals("")) {
			fishId = Integer.parseInt(request.getParameter("fishId"));
		}
		
		int beginPrice = 0;//产品初始价格
		if(request.getParameter("beginPrice")!=null && !request.getParameter("beginPrice").equals("")) {
			beginPrice = Integer.parseInt(request.getParameter("beginPrice"));
		}
		
		int endPrice = 0;//产品最终价格
		if(request.getParameter("endPrice")!=null && !request.getParameter("endPrice").equals("")) {
			endPrice = Integer.parseInt(request.getParameter("endPrice"));
		}

		int fishClassifyId = 0;//产品所属分类
		if(request.getParameter("fishClassifyId")!=null && !request.getParameter("fishClassifyId").equals("")) {
			fishClassifyId = Integer.parseInt(request.getParameter("fishClassifyId"));
		}
		
		String fishName = request.getParameter("fishName");//产品名称
		String fishAdress = request.getParameter("fishAdress");//产品分布
		String fishLifeTime = request.getParameter("fishLifeTime");//产品寿命
		String isFreeDelivery = request.getParameter("isFreeDelivery");//是否包邮
		String fishPicture = request.getParameter("fishPicture");//产品图片
		
		Details d = new Details(fishId, fishName, fishAdress, 0, beginPrice, endPrice, fishLifeTime, isFreeDelivery, fishClassifyId, fishPicture, "");
		
		FishDetailsDaoImpl fDao = new FishDetailsDaoImpl();

		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<Details> dList = fDao.selProductByPage(d, page, size);//调用实现类中的方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(dList);
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	/**
	 * 根据id删除产品
	 * @param localhost:8080/shangcheng1/adminProduct?reqName=delAdminProduct
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delAdminProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		int fid = Integer.parseInt(request.getParameter("fishDetailsId"));
		FishDetailsDaoImpl fishDao = new FishDetailsDaoImpl();//创建实现类对象
		int n = fishDao.delProductDetails(fid);//调用实现类中的删除方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if(n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
	}
	
	/**
	 * 获取产品的所有详情
	 * @param localhost:8080/shangcheng1/adminProduct?reqName=addDetails
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addDetails(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		FishDetailsDaoImpl nDao = new FishDetailsDaoImpl();
		
		String fishName = request.getParameter("fishName");
		String fishAdress = request.getParameter("fishAdress");
		String fishLifeTime = request.getParameter("fishLifeTime");
		String isFreeDelivery = request.getParameter("isFreeDelivery");
		String fishPicture = request.getParameter("fishPicture");
		int fishPrice = 0;
		if(request.getParameter("fishPrice")!=null && !request.getParameter("fishPrice").equals("")) {
			fishPrice = Integer.parseInt(request.getParameter("fishPrice"));
		}
		int fishClassifyId = 0;
		if(request.getParameter("fishClassifyId")!=null && !request.getParameter("fishClassifyId").equals("")) {
			fishClassifyId = Integer.parseInt(request.getParameter("fishClassifyId"));
		}
		
		if(fishName!=null && !fishName.equals("")) {
			List<Details> deList = nDao.getProDetailsByFName(fishName);
			if (deList.size() == 0) {
				Details t = new Details(0, fishName, fishAdress, fishPrice, fishLifeTime, isFreeDelivery, fishClassifyId, fishPicture);
				System.out.println(t);
				
				int n = nDao.addProductDetails(t);
				
				if(n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "名称已存在", ""));
			}
		}
		
		
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 获取产品的所有详情
	 * @param localhost:8080/shangcheng1/adminProduct?reqName=getProDetailsById
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProDetailsById(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		FishDetailsDaoImpl nDao = new FishDetailsDaoImpl();
		
		int FishId = Integer.parseInt(request.getParameter("fishId"));
		Details det = nDao.getProDetailsById(FishId);
		
		Gson gson = new Gson();
		String res = gson.toJson(det);
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 获取产品的所有详情
	 * @param localhost:8080/shangcheng1/adminProduct?reqName=updateProDetailsById
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateProDetailsById(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		FishDetailsDaoImpl nDao = new FishDetailsDaoImpl();
		
		int fishId = Integer.parseInt(request.getParameter("fishId"));
		String fishName = request.getParameter("fishName");
		String fishAdress = request.getParameter("fishAdress");
		int fishPrice = Integer.parseInt(request.getParameter("fishPrice"));
		String fishLifeTime = request.getParameter("fishLifeTime");
		String isFreeDelivery = request.getParameter("isFreeDelivery");
		int fishClassifyId = Integer.parseInt(request.getParameter("fishClassifyId"));
		String fishPicture = request.getParameter("fishPicture");
		
		if(fishName!=null && !fishName.equals("")) {
			List<Details> deList = nDao.getProDetailsByFName(fishName);
			System.out.println(deList);
			if (deList.size()==0) {
				Details d = new Details(fishId, fishName, fishAdress, fishPrice, fishLifeTime, isFreeDelivery, fishClassifyId, fishPicture);
				int n = nDao.updateProductDetails(d);
				
				if (n > 0) {
					out.print(Result.toClient(true, "修改成功", ""));
				} else {
					out.print(Result.toClient(false, "修改失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "产品名已存在", ""));
			}
		}
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 根据多条产品id删除产品
	 * localhost:8080/shangcheng1/adminProduct?reqName=delProbuctByIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delProbuctByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		FishDetailsDaoImpl dDao = new FishDetailsDaoImpl();
		String ids = request.getParameter("ids");
		int n = dDao.delProDetailsByIds(ids);
		
				
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
			
			out.flush();
			out.close();

	}
	
	/**
	 *  localhost:8080/shangcheng1/adminProduct?reqName=upload
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		//1创建SmartUpload对象
		SmartUpload smart = new SmartUpload();
		
		//2设置编码格式
		smart.setCharset("UTF-8");
		
		//3初始化
		smart.initialize(getServletConfig(),request,response);
		
		try {
			//4上传
			smart.upload();
			//5设置上传文件名字
			String fileName = System.currentTimeMillis() + ".jpg";
			//将文件保存到制定目录
			smart.getFiles().getFile(0).saveAs("/images/" + fileName);
			//响应
			out.print(Result.toClient(true, "上传成功",fileName));
		} catch (SmartUploadException e) {
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败","图片格式不符"));
		}
		out.flush();
		out.close();
	}

	
}
